package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.ysu.populationmanagement.constant.Errors;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.LiveCard;
import com.neusoft.ysu.populationmanagement.entity.OperationLog;
import com.neusoft.ysu.populationmanagement.mapper.LiveCardMapper;
import com.neusoft.ysu.populationmanagement.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lhn
 * @since 2025-06-19
 */
@Service
public class LiveCardServiceImpl implements ILiveCardService {

    @Autowired
    LiveCardMapper liveCardMapper;
    @Autowired
    IOperationLogService logService;
    @Autowired
    IVFloPopService vFloPopService;

    @Override
    public CommonResult apllyLiveCard(String idnum, Long pid, Long operatorId) {

        // 检查是否为流动人口
        if (vFloPopService.selectFloByIdNum(idnum).getCode() == Errors.ERROR_FLO_SELECT) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民不是流动人口", null);
        }
        // 检查是否有未作废的居住证
        List<LiveCard> liveCards = selectLiveCardByIdnum(idnum);
        if (!liveCards.isEmpty()) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民已有未作废的居住证", liveCards);
        }

        // 新增居住证
        LiveCard liveCard = new LiveCard();
        liveCard.setIdnum(idnum);
        liveCard.setAuditStatus(TabelDic.APPLY_FINISHED);
        liveCard.setApllyTime(LocalDateTime.now());
        liveCard.setPid(pid);
        liveCard.setOperatorId(operatorId);
        liveCard.setCreateTime(LocalDateTime.now());
        liveCard.setIsDelete(TabelDic.IS_DELETE_NO);
        int result = liveCardMapper.insert(liveCard);
        if (result == 0) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "新增居住证失败(数据库插入失败)", liveCard);
        }

        // 新增日志记录
        CommonResult log = logService.insertByIdnum(idnum, pid, operatorId, "申请居住证", "公安干警操作");
        if (log.getCode() == Errors.ERROR) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "新增居住证成功，新增日志记录失败", log);
        }
        return new CommonResult(Errors.SUCCESS, "新增居住证成功", liveCard);
    }

    @Override
    public CommonResult buildLiveCard(String idnum, Long pid, Long operatorId) {
        List<LiveCard> liveCardList = selectLiveCardByIdnum(idnum);
        if (liveCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民未申领居住证", null);
        }
        if (liveCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民有多个未作废的居住证", null);
        }
        LiveCard liveCard = liveCardList.get(0);
        if (!liveCard.getAuditStatus().equals(TabelDic.APPLY_FINISHED)) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民居住证处于 " + liveCard.getAuditStatus() + " 状态，请重新申请", liveCard);
        }
        liveCard.setAuditStatus(TabelDic.BUILDING);
        liveCard.setBuildTime(LocalDateTime.now());
        return new CommonResult<>(1,"制证成功",liveCardMapper.update(liveCard,new LambdaQueryWrapper<LiveCard>().eq(LiveCard::getId,liveCard.getId())) );
    }

    @Override
    public CommonResult finishLiveCard(String idnum, Long pid, Long operatorId) {
        List<LiveCard> liveCardList = selectLiveCardByIdnum(idnum);
        if (liveCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民未申领居住证", null);
        }
        if (liveCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民有多个未作废的居住证", null);
        }
        LiveCard liveCard = liveCardList.get(0);
        if (!liveCard.getAuditStatus().equals(TabelDic.BUILDING)) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民居住证处于 " + liveCard.getAuditStatus() + " 状态，请重新申请", liveCard);
        }
        liveCard.setAuditStatus(TabelDic.BUILDI_FINISHED);
        liveCard.setFinishTime(LocalDateTime.now());
        return new CommonResult<>(1,"完证成功",liveCardMapper.update(liveCard,new LambdaQueryWrapper<LiveCard>().eq(LiveCard::getId,liveCard.getId())) );

    }

    @Override
    public CommonResult sendLiveCard(String idnum, Long pid, Long operatorId) {
        List<LiveCard> liveCardList = selectLiveCardByIdnum(idnum);
        if (liveCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民未申领居住证", null);
        }
        if (liveCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民有多个未作废的居住证", null);
        }
        LiveCard liveCard = liveCardList.get(0);
        if (!liveCard.getAuditStatus().equals(TabelDic.BUILDI_FINISHED)) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民居住证处于 " + liveCard.getAuditStatus() + " 状态，请重新申请", liveCard);
        }
        liveCard.setAuditStatus(TabelDic.SEND_FINISHED);
        liveCard.setSendTime(LocalDateTime.now());
        return new CommonResult<>(1,"发证成功",liveCardMapper.update(liveCard,new LambdaQueryWrapper<LiveCard>().eq(LiveCard::getId,liveCard.getId())) );

    }

    @Override
    public CommonResult lossLiveCard(String idnum, Long pid, Long operatorId) {
        List<LiveCard> liveCardList = selectLiveCardByIdnum(idnum);
        if (liveCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民未申领居住证", null);
        }
        if (liveCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民有多个未作废的居住证", null);
        }
        LiveCard liveCard = liveCardList.get(0);
        if (!liveCard.getAuditStatus().equals(TabelDic.SEND_FINISHED)) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民居住证处于 " + liveCard.getAuditStatus() + " 状态，请重新申请", liveCard);
        }
        liveCard.setAuditStatus(TabelDic.LOSS);
        liveCard.setLossTime(LocalDateTime.now());
        return new CommonResult<>(1,"居住证已挂失",liveCardMapper.update(liveCard,new LambdaQueryWrapper<LiveCard>().eq(LiveCard::getId,liveCard.getId())) );

    }

    @Override
    public CommonResult deleteLiveCard(String idnum, Long pid, Long operatorId) {
        List<LiveCard> liveCardList = selectLiveCardByIdnum(idnum);
        if (liveCardList.isEmpty()) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民未申领居住证", null);
        }
        if (liveCardList.size() != 1) {
            return new CommonResult(Errors.ERROR_FLO_PERMIT, "该居民有多个未作废的居住证", null);
        }
        LiveCard liveCard = liveCardList.get(0);

        liveCard.setAuditStatus(TabelDic.BUILDI_FINISHED);
        liveCard.setValidEndTime(LocalDateTime.now());
        liveCard.setIsDelete(TabelDic.IS_DELETE_YES);

        return new CommonResult<>(1,"居住证已作废",liveCardMapper.update(liveCard,new LambdaQueryWrapper<LiveCard>().eq(LiveCard::getId,liveCard.getId())) );

    }

    @Override
    public List<LiveCard> selectLiveCardByIdnum(String idnum) {
        LambdaQueryWrapper<LiveCard> lq = new LambdaQueryWrapper<>();
        lq.eq(LiveCard::getIdnum, idnum);
        lq.eq(LiveCard::getIsDelete, TabelDic.IS_DELETE_NO);
        List<LiveCard> liveCardList = liveCardMapper.selectList(lq);
        return liveCardList;
    }

}
